<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: magic_renew.php 6752 2010-03-25 08:47:54Z cnteacher $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

if(submitcheck('usesubmit')) {

	if(empty($pid)) {
		showmessage('magics_info_nonexistence');
	}

	$post = getpostinfo($pid, 'pid', array('p.tid', 'p.fid', 'p.authorid', 'first', 'anonymous'));
	checkmagicperm($magicperm['forum'], $post['fid']);

	$query = DB::query("SELECT username FROM ".DB::table('common_member')." WHERE uid='$post[authorid]'");
	$author = daddslashes(DB::result($query, 0), 1);
	$thread = getpostinfo($post['tid'], 'tid', array('tid', 'subject', 'author', 'replies', 'lastposter'));
	$posttale = getposttablebytid($post['tid']);
	if($post['first']) {
		$lastposter = $thread['replies'] > 0 ? $thread['lastposter'] : $author;
		DB::query("UPDATE ".DB::table($posttale)." SET anonymous='0' WHERE tid='$post[tid]' AND first='1'");
		updatemagicthreadlog($post['tid'], $magicid, $magic['identifier'], '0', '1');
	} else {
		$lastposter = $author;
		$author = $thread['author'];
		DB::query("UPDATE ".DB::table($posttale)." SET anonymous='0' WHERE pid='$pid'");
	}

	$query = DB::query("SELECT lastpost FROM ".DB::table('forum_forum')." WHERE fid='$post[fid]'");
	$forum['lastpost'] = explode("\t", DB::result($query, 0));

	if($thread['subject'] == $forum['lastpost'][1] && ($forum['lastpost'][3] == '' && $post['anonymous'])) {
		$lastpost = "$thread[tid]\t$thread[subject]\t$_G[timestamp]\t$lastposter";
		DB::query("UPDATE ".DB::table('forum_forum')." SET lastpost='$lastpost' WHERE fid='$post[fid]'", 'UNBUFFERED');
	}

	DB::query("UPDATE ".DB::table('forum_thread')." SET author='$author', lastposter='$lastposter', moderated='1' WHERE tid='$post[tid]'");

	usemagic($magicid, $magic['num']);
	updatemagiclog($magicid, '2', '1', '0', '0', $pid);

	if($thread['authorid'] != $_G['uid']) {
		sendnotice($thread['authorid'], 'magic_thread_anonymous', 'systempm');
	}

	showmessage('magics_operation_succeed', '', array('succeed' => 1));

}

function showmagic() {
	global $pid, $lang;
	magicshowtype($lang['option'], 'top');
	magicshowsetting($lang['target_pid'], 'pid', $pid, 'text');
	magicshowtype('', 'bottom');
}

?>